package java_thinking.unit_21.Exercise.thread.pool;

import java.util.concurrent.*;

/**
 * @Desc:
 * @author: cww
 * @DateTime: 2019/7/31 9:26
 */

public class ThreadPoolTest {
    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,3,60L, TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>());
        executor.execute(new Task1());
        Future<?> task1 =  executor.submit(new Task1());
        Thread thread = new Thread();
//        thread.join();
        try {
            System.out.println(task1.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        FutureTask<Integer> task = (FutureTask<Integer>) executor.submit(new TaskCallable());
        try {
            task.get(5,TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            System.out.println("ExecutionException");
        } catch (TimeoutException e) {
            System.out.println("TimeoutException");
        }
    }
}

class Task1 implements Runnable {

    /**

     */
    @Override
    public void run() {
        System.out.println("run task");
        int i = 0;
        while (i < Integer.MAX_VALUE) {
            i++;
        }
    }
}

class TaskCallable implements Callable<Integer> {

    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    public Integer call() throws Exception {
        boolean finished = false;
        while (true) {
            if (finished) {
                break;
            }
        }
        return 10;
    }
}
